package com.liaowei.post.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.SpecialColumn;
import com.liaowei.model.vo.SpecialColumnVO;
import com.liaowei.post.model.param.SaveSpecialColumnParam;
import com.liaowei.post.model.param.SpecialColumnManagePageParam;
import com.liaowei.post.model.param.SpecialColumnStatusParam;
import com.liaowei.post.service.SpecialColumnService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("special-column")
@RequiredArgsConstructor
@Validated
public class SpecialColumnController {

    private final SpecialColumnService specialColumnService;


    // 查询热门专栏
    @GetMapping("hot")
    public BaseResponse<List<SpecialColumnVO>> hot(){
        return ResultUtils.success(specialColumnService.searchHotColumnVOList());
    }

    // 查询最近访问的帖子所属的专栏
    @GetMapping("list/{userId}")
    public BaseResponse<List<SpecialColumn>> listByUserId(@PathVariable Long userId){
        return ResultUtils.success(specialColumnService.searchColumnListByUserId(userId));
    }

    // 根据id查询SpecialColumnVO
    @GetMapping("{id}")
    public BaseResponse<SpecialColumnVO> columnVOById(@PathVariable Long id){
        return ResultUtils.success(specialColumnService.searchColumnVOById(id));
    }

    // 后台-分页查询专栏
    @PostMapping("manage/page")
    public BaseResponse<IPage<SpecialColumnVO>> columnManagePage(@RequestBody
                                                                 @Validated
                                                                 SpecialColumnManagePageParam pageParam) {
        return ResultUtils.success(specialColumnService.searchColumnManagePage(pageParam));
    }

    // 查询全部的专栏
    @GetMapping("list")
    public BaseResponse<List<SpecialColumn>> list(){
        return ResultUtils.success(specialColumnService.searchSpecialColumnList());
    }

    // 分页查询专栏
    @GetMapping("page")
    public BaseResponse<IPage<SpecialColumnVO>> columnPage(
            @NotNull(message = "分页参数不能为空")
            @Min(value = 1, message = "当前页最小为1")
            Integer pageNum,
            @NotNull(message = "分页参数不能为空")
            @Min(value = 1, message = "分页大小最小为1")
            Integer pageSize) {
        return ResultUtils.success(specialColumnService.searchColumnPage(pageNum, pageSize));
    }

    // 修改专栏状态 只有具有专栏修改权限才能进入 管理员具备
    @PutMapping("manage/status")
    public BaseResponse<String> updateSpecialColumnStatus(@RequestBody
                                                          @Validated
                                                          SpecialColumnStatusParam statusParam) {
        return ResultUtils.success(specialColumnService.updateSpecialColumnStatus(statusParam));
    }

    // 新增专栏信息
    @PostMapping
    public BaseResponse<String> addSpecialColumn(@RequestBody
                                                 @Validated
                                                 SaveSpecialColumnParam specialColumnParam) {
        return ResultUtils.success(specialColumnService.addSpecialColumn(specialColumnParam));
    }
}
